package com.tanhua.server.controller;

import com.tanhua.server.service.UsersService;
import com.tanhua.server.vo.CountsVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.SettingsVo;
import com.tanhua.server.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * [我的] 模块
 */
@RestController
@RequestMapping("users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    /**
     * 是否喜欢
     */
    @GetMapping("{userId}/alreadyLove")
    public ResponseEntity<Boolean> alreadyLove(@PathVariable("userId") Long userId) {
        return ResponseEntity.ok(true);
    }

    /**
     * 用户资料 - 读取
     */
    @GetMapping
    public ResponseEntity<UserInfoVo> queryUserInfo(@RequestParam(value = "userID", required = false) String userID,
                                                    @RequestParam(value = "huanxinID", required = false) String huanxinID) {
        //用户第一次 接收到 环信推来的信息 ，不带用户信息
        UserInfoVo userInfoVo = this.usersService.queryUserInfo(userID, huanxinID);
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 用户资料 - 更新信息
     */
    @PutMapping
    public ResponseEntity<Void> updateUserInfo(@RequestBody UserInfoVo userInfoVo) {
        this.usersService.updateUserInfo(userInfoVo);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     */
    @GetMapping("counts")
    public ResponseEntity<CountsVo> queryCounts() {
        CountsVo countsVo = this.usersService.queryCounts();
        return ResponseEntity.ok(countsVo);
    }

    /**
     * 互相关注、我关注、粉丝、谁看过我 - 翻页列表
     * type： 1 互相关注 2 我关注 3 粉丝 4 谁看过我
     */
    @GetMapping("friends/{type}")
    public ResponseEntity<PageResult> queryLikeList(@PathVariable("type") String type,
                                                    @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                    @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize,
                                                    @RequestParam(value = "nickname", required = false) String nickname) {
        PageResult pageResult = this.usersService.queryLikeList(Integer.valueOf(type), page, pagesize, nickname);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 喜欢
     */
    @PostMapping("fans/{userId}")
    public ResponseEntity<Void> likeFan(@PathVariable("userId") Long userId) {
        this.usersService.likeFan(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消喜欢
     */
    @DeleteMapping("like/{userId}")
    public ResponseEntity<Void> disLike(@PathVariable("userId") Long userId) {
        this.usersService.disLike(userId);
        return ResponseEntity.ok(null);
    }


    /**
     * 查询配置
     */
    @GetMapping("settings")
    public ResponseEntity<SettingsVo> querySettings() {
        SettingsVo settingsVo = this.usersService.querySettings();
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 设置陌生人问题
     */
    @PostMapping("questions")
    public ResponseEntity<Void> saveQuestions(@RequestBody Map<String, String> param) {
        String content = param.get("content");
        this.usersService.saveQuestions(content);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询黑名单
     */
    @GetMapping("blacklist")
    public ResponseEntity<PageResult> queryBlacklist(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        PageResult pageResult = this.usersService.queryBlacklist(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 移除黑名单
     */
    @DeleteMapping("blacklist/{userId}")
    public ResponseEntity<Void> delBlacklist(@PathVariable("userId") Long userId) {
        this.usersService.delBlacklist(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 更新通知设置
     */
    @PostMapping("notifications/setting")
    public ResponseEntity<Void> updateNotification(@RequestBody Map<String, Boolean> param) {
        Boolean likeNotification = param.get("likeNotification");
        Boolean pinglunNotification = param.get("pinglunNotification");
        Boolean gonggaoNotification = param.get("gonggaoNotification");

        this.usersService.updateNotification(likeNotification, pinglunNotification, gonggaoNotification);
        return ResponseEntity.ok(null);
    }

}
